#include<bits/stdc++.h>
#define MAXN 100100
using namespace std;
int n,m;
int a_r[MAXN];
int c[MAXN];
bool d[20][MAXN];
int K[4];
int a[MAXN];
int win[20][MAXN];
bool check(int x){
    while(x){
        if(x&1)return false;
        x>>=1;
    }
    return true;
}
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>a_r[i];
    for(int i=1;i<=m;i++)cin>>c[i];
    int k=0;
    int tmp=1;
    while(tmp<=n){
        tmp<<=1;
        k++;
    }
    for(int i=1;i<=k;k++){
        tmp/=2;
        for(int j=1;j<=tmp;j++){
            char ch;
            cin>>ch;
            d[i][j]=ch-'0';
        }
    }
    bool flag=0;
    for(int i=1;i<=m;i++)if(!check(c[i]))flag=1;
    if(flag)return 0;
    int T;
    cin>>T;
    while(T--){
        for(int i=0;i<4;i++)cin>>K[i];
        for(int i=1;i<=n;i++)a[i]=a_r[i]^K[i%4];
        for(int i=1;i<=n;i++)win[0][i]=i;
        for(int i=1;i<=k;i++){
            for(int j=1;j<=(1<<(k-1));j++){
                if(!d[i][j]){
                    if(a[win[i-1][2*j-1]]<j)win[i][j]=win[i-1][2*j-1];
                    else win[i][j]=win[i-1][2*j];
                }
                else{
                   if(a[win[i-1][2*j]]<j)win[i][j]=win[i-1][2*j];
                   else win[i][j]=win[i-1][2*j-1]; 
                }
            }
        }
        int ans=0;
        for(int i=1;i<=m;i++){
            int k=0;
            int tmp=c[i];
            while(tmp){
                k++;
                tmp>>=1;
            }
        }
        cout<<win[k][1]<<endl;
        memset(win,0,sizeof(cin));
    }
    return 0;
}